//
//  ViewController.m
//  HomeWork-19Brand
//
//  Created by 鹏飞罗 on 16/8/9.
//  Copyright © 2016年 罗鹏飞. All rights reserved.
//

#import "ViewController.h"
#import "CarCollectionViewCell.h"
#import "Car.h"
#import "CustomCollectionViewLayout.h"
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UICollectionView *myCollectionView;
@property (nonatomic, strong) NSMutableArray *allCarArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _allCarArray = [NSMutableArray array];
    // Do any additional setup after loading the view, typically from a nib.
    [self handleData];
//    UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.myCollectionView.collectionViewLayout;
//    CGFloat width = CGRectGetWidth(self.myCollectionView.frame) / 5;
//    CGFloat height = 100;
//    
//    _myCollectionView.backgroundColor = [UIColor greenColor];
//    layout.itemSize = CGSizeMake(width, height);
//    layout.minimumLineSpacing = 0;
//    layout.minimumInteritemSpacing = 0;

    CustomCollectionViewLayout *layout = (CustomCollectionViewLayout *)
    self.myCollectionView.collectionViewLayout;
    layout.cellWidth = self.view.frame.size.width / 5;
    layout.cellHeight = 100;
}
- (void)handleData {
    NSString *carPath = [[NSBundle mainBundle] pathForResource:@"HotBrand" ofType:@"json"];
    NSData *carData = [NSData dataWithContentsOfFile:carPath];
    NSError *error = nil;
    NSDictionary *carDic = [NSJSONSerialization JSONObjectWithData:carData options:NSJSONReadingMutableContainers error:&error];
    _allCarArray = carDic[@"result"] [@"list"];
    NSLog(@"%@",_allCarArray);
}
- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
   return  _allCarArray.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CarCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    Car *car = [Car carWithDictionary:_allCarArray[indexPath.row]];
    cell.carImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:car.img]]];
    cell.carLabel.text = car.name;
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
